﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;

public partial class Admin_PreviewAdjustment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Id = Request.QueryString["AdjustmentId"].ToString();
            Preview(Id);
        }
    }
    private void Preview(string Id)
    {
        Adjustment obj = DataRepository.AdjustmentProvider.GetByAdjustmentId(Id);
        DataRepository.AdjustmentProvider.DeepLoad(obj);
        TList<AdjustmentDetails> lstDetail = DataRepository.AdjustmentDetailsProvider.GetByAdjustmentId(Id);

        DSAdjustment.AdjustmentDataTable dtAdjustment = new DSAdjustment.AdjustmentDataTable();
        DSAdjustment.AdjustmentRow dtAdjustmentRow = dtAdjustment.NewAdjustmentRow();
        dtAdjustmentRow.AdjustmentId = Id;
        dtAdjustmentRow.AdjustmentDate = obj.AdjustmentDate;
        dtAdjustmentRow.TotalAmountIn = obj.TotalAmountIn;
        dtAdjustmentRow.TotalAmountOut = obj.TotalAmountOut;
        dtAdjustmentRow.PreparedBy = obj.PreparedBySource.Name;
        dtAdjustmentRow.IsApproved = obj.IsApproved;
        dtAdjustmentRow.IsComplete = obj.IsComplete;
        dtAdjustmentRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtAdjustmentRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;

        dtAdjustment.AddAdjustmentRow(dtAdjustmentRow);

        DSAdjustment.AdjustmentDetailsDataTable dtAdjustmentDetails = new DSAdjustment.AdjustmentDetailsDataTable();
        DSAdjustment.AdjustmentDetailsRow dtAdjustmentDetailsRow;
        foreach (AdjustmentDetails AdjustmentDetailItem in lstDetail)
        {
            DataRepository.AdjustmentDetailsProvider.DeepLoad(AdjustmentDetailItem);
            dtAdjustmentDetailsRow = dtAdjustmentDetails.NewAdjustmentDetailsRow();
            dtAdjustmentDetailsRow.StockOutDetailId = AdjustmentDetailItem.StockOutDetailId;
            dtAdjustmentDetailsRow.AdjustmentId = AdjustmentDetailItem.AdjustmentId;
            dtAdjustmentDetailsRow.DrugId = AdjustmentDetailItem.DrugId;
            dtAdjustmentDetailsRow.DrugName = AdjustmentDetailItem.DrugIdSource.DrugName;
            dtAdjustmentDetailsRow.Quantity = AdjustmentDetailItem.Quantity;
            dtAdjustmentDetailsRow.AdjustQuantity = AdjustmentDetailItem.AdjustQuantity;
            dtAdjustmentDetailsRow.FinalQuantity = AdjustmentDetailItem.FinalQuantity;
            dtAdjustmentDetailsRow.Remark = AdjustmentDetailItem.Remark;
            dtAdjustmentDetails.AddAdjustmentDetailsRow(dtAdjustmentDetailsRow);
        }

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSAdjustment_Adjustment", dtAdjustment));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSAdjustment_AdjustmentDetails", dtAdjustmentDetails));
        ReportViewer1.DataBind();
    }
}
